﻿var lastSortColumn = "";
var lastSortDirection = "";
var loadingMessage = "";
var deleteConfirmationMessage = "";
var deleteConfirmationWithChildsMessage = "";
var deletingReplacementID;
var deletePopupID = "DeleteConfirmation";
deletingReplacementID = -1;

function initList() {

    $("th").live("click", function () {
        $(this).find("a").click();
    });

    //styled checkbox
    $('input').checkBox();
    $('#toggle-all').click(function () {
        $('#toggle-all').toggleClass('toggle-checked');
        $('#mainform input[type=checkbox]').checkBox('toggle');
        return false;
    });

    //styled tooltip
    $('a.info-tooltip ').tooltip({
        track: true,
        delay: 0,
        fixPNG: true,
        showURL: false,
        showBody: " - ",
        top: -35,
        left: 5
    });

    $("a[id^='ListAction_Sort']").live("click", function () {
        $("#List").mask(loadingMessage + "...");
        $("#List").load($(this).attr("href"), function (response, status, xhr) {
            $("#List").unmask();
        });
        return false;
    });

    $("a[id^='ListAction_Delete']").live("click", function () {

        deletingReplacementID = $(this)[0].id.replace("ListAction_Delete_", "");

        $.colorbox({ width: "900px", inline: true, href: "#" + deletePopupID, transition: "none" });

        return false;
    });

    $("a[id^='ListAction_Edit']").live("click", function () {

        $("#List").mask(loadingMessage + "...");

        $.ajax(
        {
            url: $(this).attr("href"),
            type: "GET",
            cache: false,
            success: function (result) {
                $("#List").unmask();
                $("#PopupContent").html(result);
                //$("#" + editPopupID).dialog("open");
                $.colorbox({ width: "900px", height: "640px", inline: true, href: "#PopupContent" });
                Sys.Mvc.FormContext._Application_Load();
            },
            complete: function (xhr, status, err) {
                if (status == "error") {
                    $("#List").unmask();
                    handleGlobalError(xhr, status, err);
                }
            }
        });

        return false;
    });

    $("a[id^='ListAction_Page']").live("click", function () {
        $("#List").mask(loadingMessage + "...");
        $("#List").load($(this).attr("href"), function (response, status, xhr) {
            $("#List").unmask();
        });
        return false;
    });

    $("#Create").live("click", function () {
        $("#List").mask(loadingMessage + "...");
        $.ajax(
        {
            url: $(this).attr("href"),
            type: "GET",
            cache: false,
            success: function (result) {
                $("#List").unmask();
                $("#PopupContent").html(result);
                //                $("#PopupContent").dialog("open");
                $.colorbox({ width: "900px", height: "640px", inline: true, href: "#PopupContent" });
                Sys.Mvc.FormContext._Application_Load();
            },
            complete: function (xhr, status, err) {
                if (status == "error") {
                    $("#List").unmask();
                    handleGlobalError(xhr, status, err);
                }
            }
        });

        return false;
    });
}

function closePopupAndRefresh(insert) {
    $.colorbox.close();
    ShowList();
}

function closePopUp() {
    $.colorbox.close();
}

function setGridOrderClasses() {
    $("a[id^='ListAction_Sort']").each(function () {
        if ($(this).attr("id").indexOf("_" + lastSortColumn) > 0) {
            $(this).parent().addClass("table-header-repeat-selected"); //.addClass(lastSortDirection)

        }
        else {
            $(this).parent().addClass("table-header-repeat");
        }
    });
    return false;
}

